package com.bci.cmpp.message;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.bci.cmpp.util.ASCIIEncoding;
import com.bci.cmpp.util.AlphabetEncoding;
import com.bci.cmpp.util.CMPPIO;
import com.bci.cmpp.util.GBKEncoding;
import com.bci.cmpp.util.HPRoman8Encoding;
import com.bci.cmpp.util.Latin1Encoding;
import com.bci.cmpp.util.UCS2Encoding;
import com.bci.cmpp.util.UTF16Encoding;

public class UserDataMessage {

	private int userDataLen;

	private int dataCoding;

	// user data
	private byte[] userData;

	// sm data
	private byte[] smData;

	private String smText;

	// user data header
	private List<InformationElement> elements = new ArrayList<InformationElement>();

	public int getDataCoding() {
		return dataCoding;
	}

	public int getUserDataLen() {
		return userDataLen;
	}

	public byte[] getUserData() {
		return userData;
	}

	public byte[] getSmData() {
		return smData;
	}

	public String getSmText() {
		AlphabetEncoding alphabetEncoding = ASCIIEncoding.getInstance();
		try {
			if (dataCoding == (UCS2Encoding.getInstance().getDataCoding()))
				alphabetEncoding = UCS2Encoding.getInstance();
			else if (dataCoding == (GBKEncoding.getInstance().getDataCoding()))
				alphabetEncoding = GBKEncoding.getInstance();
			else if (dataCoding == (HPRoman8Encoding.getInstance()
					.getDataCoding()))
				alphabetEncoding = HPRoman8Encoding.getInstance();
			else if (dataCoding == (Latin1Encoding.getInstance()
					.getDataCoding()))
				alphabetEncoding = Latin1Encoding.getInstance();
			else if (dataCoding == (UTF16Encoding.getInstance().getDataCoding()))
				alphabetEncoding = UTF16Encoding.getInstance();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		this.smText = alphabetEncoding.decodeString(this.smData);
		return smText;
	}

	public List<InformationElement> getElements() {
		return elements;
	}

	public UserDataMessage(byte[] buf, int dataCoding) {
		this.userData = buf;
		this.dataCoding = dataCoding;
		read(buf);
	}

	public void read(byte[] buf) {
		this.userDataLen = CMPPIO.bytesToInt(buf, 0, 1);
		this.smData = new byte[buf.length - this.userDataLen - 1];
		if (this.smData.length > 0)
			System.arraycopy(buf, this.userDataLen + 1, smData, 0,
					smData.length);
		int pos = 1;
		while (pos < this.userDataLen + 1) {
			InformationElement e = new InformationElement();
			e.setElement(CMPPIO.bytesToInt(buf, pos, 1));
			pos++;
			e.setElementLen(CMPPIO.bytesToInt(buf, pos, 1));
			pos++;
			byte[] b = new byte[e.getElementLen()];
			System.arraycopy(buf, pos, b, 0, b.length);
			e.setElementData(b);
			pos += b.length;
			this.elements.add(e);
		}
	}

	public static void main(String[] args) {
		String ss = "06080400290908" + "4e0b9a915175548c4ed676845b9d"
				+ "9a6c30020020000a202620262026"
				+ "20260020000a2026202620262026"
				+ "0020000a9a9151756b7b6b7b5730"
				+ "76ef77404ed676849a6cff0c7a81"
				+ "713663ea4f4f5b83768453cc8033"
				+ "ff0c6c1451b251b276848bf4ff1a"
				+ "0020000a201c6211518d8bf44e00"
				+ "904dff0c5e264e004e2a65c576844eba6765ff0c";
		byte buf[] = com.bci.cmpp.example.CMPPAPIExample.hexToBytes(ss);
		UserDataMessage mc = new UserDataMessage(buf, 0x08);
		System.out.println(mc.getSmText());
	}
}
